package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wenwan.manage.mapper.GoodsMapper;
import com.wenwan.manage.pojo.Goods;
import com.wenwan.manage.pojo.LimitPage;
import com.wenwan.manage.service.GoodsService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize) {
		// TODO Auto-generated method stub
		// DataToObject<List<Goods>> dto = new DataToObject<List<Goods>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);

		List<Goods> data = goodsMapper.selectAll(map);
		LimitPage limitPage = new LimitPage();

		limitPage.setCurrentPage(currentPage);
		limitPage.setPageSize(pageSize);

		Integer maxSize = goodsMapper.selectMaxSize(map);
		limitPage.setMaxSize(maxSize);

		// dto.setData(data);
		// dto.setLimitPage(limitPage);
		dto.put("data", data);
		dto.put("limitPage", limitPage);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Goods> dto = new DataToObject<Goods>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("id", id);

		Goods data = goodsMapper.selectOneById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> deleteById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("id", id);

		Boolean data = goodsMapper.deleteById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> create(String userId, String goodsCategorySecondId, String title, Double price,
			String info, String imagesCarousel, String imagesInfo) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (goodsCategorySecondId != null && !goodsCategorySecondId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "goodsCategorySecondId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("goodsCategorySecondId" +
			// StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (title != null && title.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "title" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("title" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (info != null && info.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "info" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("info" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		try {

			new Gson().fromJson(imagesCarousel, new TypeToken<List<String>>() {
			}.getType());

		} catch (Exception e) {
			// TODO: handle exception
			dto.put("data", false);
			dto.put("msg", "imagesCarousel" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("imagesCarousel" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		try {

			new Gson().fromJson(imagesInfo, new TypeToken<List<String>>() {
			}.getType());

		} catch (Exception e) {
			// TODO: handle exception
			dto.put("data", false);
			dto.put("msg", "imagesInfo" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("imagesInfo" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("id", Util.UUID());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("userId", userId);
		map.put("goodsCategorySecondId", goodsCategorySecondId);
		map.put("title", title);
		map.put("price", price);
		map.put("info", info);
		map.put("imagesCarousel", imagesCarousel);
		map.put("imagesInfo", imagesInfo);

		Boolean data = goodsMapper.create(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> update(String id, String userId, String goodsCategorySecondId, String title,
			Double price, String info, String imagesCarousel, String imagesInfo, Integer countReading,
			Integer countShare, Integer countWell) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (goodsCategorySecondId != null && !goodsCategorySecondId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "goodsCategorySecondId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("goodsCategorySecondId" +
			// StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (title != null && title.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "title" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("title" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (info != null && info.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "info" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("info" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		try {

			new Gson().fromJson(imagesCarousel, new TypeToken<List<String>>() {
			}.getType());

		} catch (Exception e) {
			// TODO: handle exception
			dto.put("data", false);
			dto.put("msg", "imagesCarousel" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("imagesCarousel" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		try {

			new Gson().fromJson(imagesInfo, new TypeToken<List<String>>() {
			}.getType());

		} catch (Exception e) {
			// TODO: handle exception
			dto.put("data", false);
			dto.put("msg", "imagesInfo" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("imagesInfo" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("id", id);
		map.put("updateTime", Util.getTime());
		map.put("userId", userId);
		map.put("goodsCategorySecondId", goodsCategorySecondId);
		map.put("title", title);
		map.put("price", price);
		map.put("info", info);
		map.put("imagesCarousel", imagesCarousel);
		map.put("imagesInfo", imagesInfo);
		map.put("countReading", countReading);
		map.put("countShare", countShare);
		map.put("countWell", countWell);

		Boolean data = goodsMapper.update(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
